
#include <string>
#include <iostream>
#include <cctype>

#include "Scanner.h"
#include "ErrorHandle.h"



using namespace std;

CScanner::CScanner(CScannerRulePtr pScannerRule)
{
    LOG("Initialize Scanner");
	m_pScannerRule = pScannerRule;
}

CScanner::~CScanner()
{
	LOG("Destruct CScanner");
}

CTerminateTokenPtr CScanner::GetToken()
{
	CTerminateTokenPtr pToken;
	// 
	WFX_ASSERT(!m_CodeStack.empty());

	// Get the Top Code
	CCodePtr pCode = m_CodeStack.top();

	m_pScannerRule->SkipSpace(pCode);

	if (pToken = m_pScannerRule->ScanSpecialToken(pCode))
	{

	}
	else if (pToken = m_pScannerRule->ScanWord(pCode))
	{

	}
	else if (pToken = m_pScannerRule->ScanNumber(pCode))
	{

	}
	else
	{
		throw scanner_exception() << error_message("Unrecognizable Character");
	}

	LOG("Scanner : GetToken Address: %p, Type:%s ", pToken.get(), TOKEN_DEBUG_STRING(pToken->GetType()).c_str());
	return pToken;
}




void CScanner::PushCode( CCodePtr pCode )
{
    m_CodeStack.push(pCode);
}


CTerminateTokenPtr CScanner::PeekToken(unsigned int index /*= 0*/)
{
    // Get the Next token and add it to cache
    // until we are able to peek to the index
    CTerminateTokenPtr pToken = CTokenFactory::createToken<CTerminateToken>();
    while (m_TokenCache.size() <= index)
    {

        pToken = GetToken();
        m_TokenCache.push_back(pToken);
    }

    return m_TokenCache[index];
}


